package com.xbongbong.saas.model.impl;

import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.saas.domain.dao.ProductBaseInfoDao;
import com.xbongbong.saas.domain.entity.ProductBaseInfoEntity;
import com.xbongbong.saas.model.ProductBaseInfoModel;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author Jian.Wang
 * @Date 19:42 2019/1/29
 * @since v1.0
 **/
@Service("ProductBaseInfoModel")
public class ProductBaseInfoModelImpl implements ProductBaseInfoModel{
    @Resource
    private ProductBaseInfoDao productBaseInfoDao;
    /**
     * 插入数据
     *
     * @param productBaseInfo 表单实体数据
     * @return Integer
     * @author Jian.Wang
     * @date 2019/2/21 16:51
     * @since v1.0
     **/
    @Override
    public Integer insert(ProductBaseInfoEntity productBaseInfo) {
        long now = DateTimeUtil.getInt();
        productBaseInfo.setAddTime(now);
        productBaseInfo.setUpdateTime(now);
        return productBaseInfoDao.insert(productBaseInfo);
    }

    /**
     * 更新数据
     *
     * @param productBaseInfo 表单实体数据
     * @return Integer
     * @author Jian.Wang
     * @date 2019/2/21 16:51
     * @since v1.0
     **/
    @Override
    public Integer update(ProductBaseInfoEntity productBaseInfo) {
        long now = DateTimeUtil.getInt();
        productBaseInfo.setUpdateTime(now);
        return productBaseInfoDao.update(productBaseInfo);
    }

    @Override
    public Integer save(ProductBaseInfoEntity productBaseInfo) {
        Long id = productBaseInfo.getId();
        if (id == null || id == 0L) {
            return insert(productBaseInfo);
        } else {
            return update(productBaseInfo);
        }
    }

    /**
     * 删除单条数据
     *
     * @param key    主键
     * @param corpid 公司ID
     * @return Integer
     * @author Jian.Wang
     * @date 2019/2/21 16:51
     * @since v1.0
     **/
    @Override
    public Integer deleteByKey(Long key, String corpid) {
        return productBaseInfoDao.deleteByKey(key,corpid);
    }

    /**
     * 根据索引查询数据
     *
     * @param key    主键
     * @param corpid 公司ID
     * @return ProductBaseInfoEntity
     * @author Jian.Wang
     * @date 2019/2/21 16:51
     * @since v1.0
     **/
    @Override
    public ProductBaseInfoEntity getByKey(Long key, String corpid) {
        return productBaseInfoDao.getByKey(key,corpid);
    }

    @Override
    public List<ProductBaseInfoEntity> getByRefIdAndRefType(Long refId, Integer refType, String corpid) {
        Map<String, Object> params = new HashMap<>();
        params.put("corpid", corpid);
        params.put("refId", refId);
        params.put("refType", refType);
        params.put("del", 0);
        return findEntitys(params);
    }

    /**
     * 根据索引查询数据
     *
     * @param param 查询参数
     * @return List<ProductBaseInfoEntity>
     * @author Jian.Wang
     * @date 2019/2/21 16:51
     * @since v1.0
     **/
    @Override
    public List<ProductBaseInfoEntity> findEntitys(Map<String, Object> param) {
        return productBaseInfoDao.findEntitys(param);
    }

    /**
     * 获取数据条数
     *
     * @param param 查询参数
     * @return Integer
     * @author Jian.Wang
     * @date 2019/1/23 20:00
     * @since v1.0
     **/
    @Override
    public Integer getEntitysCount(Map<String, Object> param) {
        return productBaseInfoDao.getEntitysCount(param);
    }

    /**
     * 根据关联对象id和公司id删除数据
     *
     * @param corpid  公司ID
     * @param refIdIn 关联对象ID集合
     * @param refType 对象类型
     * @return Integer
     * @author Jian.Wang
     * @date 2019/2/21 16:51
     * @since v1.0
     **/
    @Override
    public Integer deleteByRefIds(String corpid, List<Long> refIdIn, Integer refType) {
        if (CollectionUtils.isEmpty(refIdIn)) {
            return 0;
        }
        return productBaseInfoDao.deleteByRefIds(corpid,refIdIn,refType);
    }

    /**
     *
     * @param corpid 公司Id
     * @param refType 业务类型
     * @param refIdIn 关联对象集合
     * @return List<ProductBaseInfoEntity>
     * @author Jian.Wang
     * @date 2019/2/21 16:51
     * @since v1.0
     */
    @Override
    public List<ProductBaseInfoEntity> getProductBaseInfoList(String corpid, Integer refType, Collection refIdIn) {
        List<ProductBaseInfoEntity> productBaseInfoList = new ArrayList<>();
        if (refIdIn.isEmpty()) {
            return productBaseInfoList;
        }
        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put("corpid", corpid);
        param.put("del", 0);
        param.put("refType", refType);
        param.put("refIdIn", refIdIn);
        productBaseInfoList = findEntitys(param);
        return productBaseInfoList;
    }

    /**
     *
     * @param refId 关联对象ID
     * @param corpid 公司ID
     * @param refType 业务类型
     * @return List<ProductBaseInfoEntity>
     * @author Jian.Wang
     * @date 2019/2/21 16:51
     * @since v1.0
     */
    public List<ProductBaseInfoEntity> getByRefId(Integer refId, String corpid, Integer refType){
        Map<String, Object> params = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        params.put("corpid", corpid);
        params.put("refId", refId);
        params.put("refType", refType);
        params.put("del", 0);
        return findEntitys(params);
    }

    @Override
    public void insertBatch(List<ProductBaseInfoEntity> productBaseInfoEntityList) {
        if (CollectionUtils.isEmpty(productBaseInfoEntityList)) {
            return;
        }
        productBaseInfoDao.insertBatch(productBaseInfoEntityList);
    }

    @Override
    public void updateBatch(List<ProductBaseInfoEntity> productBaseInfoEntityList, String corpid) {
        if (CollectionUtils.isEmpty(productBaseInfoEntityList)) {
            return;
        }
        productBaseInfoDao.updateBatch(productBaseInfoEntityList, corpid);
    }
}
